package com.atguigu.eduservice.controller;


import cn.hutool.core.util.StrUtil;
import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.CustomException;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author test.java
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {

	@Resource
	private EduVideoService eduVideoService;

	@Resource
	private VodClient vodClient;

	@DeleteMapping("/{id}")
	public Result deleteVideo(@PathVariable("id") String id){

		EduVideo eduVideo = eduVideoService.getById(id);
		String videoSourceId = eduVideo.getVideoSourceId();
		if (!StrUtil.isEmpty(videoSourceId)) {
			//删除视频
			Result result = vodClient.removeVideo(id);
			//判断删除视频是否发生了熔断
			if(result.getCode() == 20001){
				//表示发生了熔断
				throw new CustomException (20001,"发生了熔断");

			}
			//删除小节
			boolean b = eduVideoService.removeById(id);
			if (b) {
				return Result.ok().message("删除成功");
			}

		}
		return Result.error().message("删除失败");
	}

}

